package com.xiaoma.myaudience.biz.task;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.model.PostCommentInfo;
import com.xiaoma.myaudience.biz.task.PostCommentTask.PostCommentStatus;
import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;

/**
 * 发帖
 * @author xali
 * 需要参数封装到 PostCommentInfo中
 * mNickname， mHidename 默认不用传递
 * mReplyWithQuote 是否为回复跟贴判断
 */
public class PostCommentTask extends BaseDataAsyncTask<PostCommentInfo, Void, PostCommentStatus> {
    private final static String TAG = "PostCommentTask";
    
    /**
     * {@inheritDoc}
     * 
     * @param context
     * @param taskType
     * @param callback
     */
    public PostCommentTask(Context context, int taskType, DataAsyncCallback callback) {
        super(context, taskType, callback);
    }
    
    @Override
    protected PostCommentStatus doInBackground(PostCommentInfo... params) {
    	
    	PostCommentStatus commentStatus = null;
    	
        if(params[0] == null) 
        	return commentStatus;
        
        PostCommentInfo postInfo = params[0];
        postInfo.mNickname = mContext.getResources().getString(R.string.app_name);
        postInfo.mHidename = "false";
        
        String url = Constant.URL_POST_COMMENT;
        Logger.i("url is "+url);
        Logger.i("mBoard:" + postInfo.mBoard);
        Logger.i("mBody:" + postInfo.mBody);
        Logger.i("mHidename:" + postInfo.mHidename);
        Logger.i("mIp:" + postInfo.mIp);
        Logger.i("mNickname:" + postInfo.mNickname);
        Logger.i("mQuote:" + postInfo.mQuote);
        Logger.i("mThreadid:" + postInfo.mThreadid);
        Logger.i("mUserid:" + postInfo.mUserid);
        Logger.i("mReplyWithQuote:" + postInfo.mReplyWithQuote);

        try {
        	commentStatus = new PostCommentStatus();
            
        	List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        	postParams.add(new BasicNameValuePair(PostCommentInfo.REPLY_BOARDID, postInfo.mBoard));
        	postParams.add(new BasicNameValuePair(PostCommentInfo.REPLY_ID, postInfo.mThreadid));
        	
        	if(postInfo.mReplyWithQuote)
        		postParams.add(new BasicNameValuePair(PostCommentInfo.QUOTE_TAG, postInfo.mQuote));
        	
        	postParams.add(new BasicNameValuePair(PostCommentInfo.BODY_TAG, postInfo.mBody));
        	postParams.add(new BasicNameValuePair(PostCommentInfo.USERID_TAG, postInfo.mUserid));
        	postParams.add(new BasicNameValuePair(PostCommentInfo.NICKNAME_TAG, postInfo.mNickname));
        	postParams.add(new BasicNameValuePair(PostCommentInfo.IP_TAG, postInfo.mIp));
        	postParams.add(new BasicNameValuePair(PostCommentInfo.HIDDENAME_TAG, postInfo.mHidename));
    		
        	JSONObject jsonObj = HttpUtils.getHttpJSONObjectResult(getHttpClient(), url, postParams, HttpUtils.POST);
        	
        	if(jsonObj != null && !jsonObj.isNull(Constant.CODE_TAG)) {
        		
        		String returnCode = jsonObj.getString(Constant.CODE_TAG);
        		commentStatus.mCodeStatus = returnCode;
        		
				if(!"1".equals(returnCode)){
	        		String returnMeg = jsonObj.getString(Constant.ERRMSG_TAG);
	        		commentStatus.mMessage = returnMeg;
				}
			
			}

        	Logger.i("code:" + commentStatus.mCodeStatus);
        	Logger.i("message:" + commentStatus.mMessage);
        	
        	return commentStatus;
        } catch (Exception e) {
            Logger.e(TAG, "doInBackground error!!!", e);
            return null;
        } finally {
            closeHttpClient();
        }
        
    }
    
    public class PostCommentStatus{
    	/**发帖标示, 1为发帖成功 ，其他为发帖失败*/
    	public String mCodeStatus;
    	/**发帖返回信息*/
    	public String mMessage;
    }
}
